<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <span>家医管理系统</span>
      </div>

      <div class="out">
        <el-button type="info" @click="loginout">退出</el-button>
      </div>
    </el-header>
    <!-- 页面主题区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '250px'">
        <el-menu
          :default-active="$route.path"
          router
          background-color="#2F3B55"
          text-color="#fff"
          active-text-color="#ffd04b"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menus"
            :key="item.id"
          >
            <template slot="title">
              <span>{{ item.name }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-submenu
              :index="subItem.id + ''"
              v-for="subItem in item.childrenNode"
              :key="subItem.id"
            >
              <template slot="title">
                <span>{{ subItem.name }}</span>
              </template>
              <!-- 三级菜单 -->
              <el-menu-item
                :index="subItems.href + ''"
                v-for="subItems in subItem.childrenNode"
                :key="subItems.id"
                @click="menuClick(subItems)"
              >
                <template slot="title">
                  <span> {{ subItems.name }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <el-tabs
          type="card"
          closable
          @tab-remove="removeTab"
          @tab-click="tabClick"
        >
          <el-tab-pane
            v-for="item in editableTabs"
            :key="item.path"
            :label="item.name"
            :name="item.path"
          >
          </el-tab-pane>
        </el-tabs>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { logout } from "@/api/family_doctor/family_doctor";
export default {
  data() {
    return {
      permissionArr: [],
      tableData: [],
      tableDatas: [],
      account: "",
      name: "",
      content: "",
      menus: this.$store.state.menus,
      defaultUrl: "",
      isCollapse: false,
      editableTabs: [],
      ifAdd: true,
      ePath: "",
    };
  },
  created() {},
  methods: {
    //退出登录，清空token
    loginout() {
      logout().then((res) => {
        window.sessionStorage.clear();
        this.$router.push("/login");
        this.activated();
      });
    },
    // 退出登录禁止浏览器后退
    activated() {
      history.pushState(null, null, document.URL);
      window.addEventListener("popstate", function () {
        history.pushState(null, null, document.URL);
      });
    },
    menuClick(value) {
      this.ifAdd = true;
      this.editableTabs.forEach((e) => {
        if (e.path == value.href) {
          this.ifAdd = false;
        }
      });
      if (this.ifAdd == true) {
        this.editableTabs.push({
          path: value.href,
          name: value.name,
        });
      }
    },
    removeTab(value) {
      this.editableTabs.forEach((e, index) => {
        if (e.path == value) {
          if (e.path == this.$route.path && this.editableTabs.length > 1) {
            if (index > 0) {
              this.$router.push(this.editableTabs[index - 1].path);
            }
            else{this.$router.push(this.editableTabs[index+1].path);}
          }
          this.editableTabs.splice(index, 1);
        }
      });
    },
    tabClick(value) {
      this.$router.push(value.name);
    },
  },
};
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #3b4966;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  font-size: 25px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #3b4966;
  .el-menu {
    border-right: 0;
  }
  .el-submenu__title {
    margin-left: -22px;
    width: 250px;
  }
}
.el-main {
  background-color: #ffffff;
}
.el-aside /deep/ .el-submenu__title {
  margin-left: -22px;
  width: 250px;
}
.el-aside /deep/ .el-menu-item {
  margin-left: -22px;
  width: 250px;
}
</style>